package com.itheima.controller;

import cn.hutool.core.util.ObjectUtil;
import com.itheima.common.R;
import com.itheima.dto.EmployeeDto.LoginDto;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import com.itheima.utils.JwtUtils;
import com.itheima.vo.EmployeeVo.LoginVo;
import io.jsonwebtoken.Claims;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author 小帅蓝
 * @Date 2022/6/12 15:07
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<LoginVo> login(@RequestBody LoginDto dto) {
        String password = dto.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LoginVo vo = employeeService.login(dto);

        if (ObjectUtil.isNull(vo)) {
            return R.error("error");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("username", dto.getUsername());
        map.put("password", password);
        map.put("token", dto.getUsername());
        String token = JwtUtils.getToken(map);

        vo.setToken(token);
        return R.success(vo);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        boolean b = JwtUtils.verifyToken(token);
        return R.success("退出成功");
    }

}
